<?php
namespace app\index\controller;
use app\admin\model\MemberModel;

class Wx extends \think\Controller
{
    public $appid = 'wxfdfc7b745bf64a1b';
    public $secret = 'afe901254efd8115d015a5bae6e1f301';
    public function checkToken(){
        //返回echostr
        $echostr = $_GET['echostr'];
        if($echostr)
        {
            echo $echostr;
            exit;
        }
    }
//生成二维码
    public function index(){
//        $redirect_url = urlencode('http://' . $_SERVER['SERVER_NAME'] . '/index/Wx/user');
//        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=" . $redirect_url . "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appid.'&redirect_uri=http%3a%2f%2fdraw.lwz123.xin%2findex%2fWx%2fuser&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
        $file = 'http://' . $_SERVER['SERVER_NAME'] . '/' . Qrcode('code',$url);
        $this->assign('file' , $file);
        return $this->fetch();
    }
    public function user(){
        session('user_id',0);   //清除登录状态
        $data = $this->request->param();
        $code = $data['code'];

        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->secret.'&code='.$code.'&grant_type=authorization_code';

        $access = json_decode(httpGet($url), true );
        if($access['openid']){
            $r = Db('member')->where(['openid'=>$access['openid']])->find();
//print_r($r);die;
            if($r){  //有注册完成
            	if($r['wx_code']){
            		session('user_id',$r['id']);
                	header('location:'.url('index/Index/index'));
                	die;
            	}else{
                    session('user_id',$r['id']);
                    header('location:'.url('index/User/reg'));
                    die;
                }
            }
            $userInfo = json_decode(httpGet('https://api.weixin.qq.com/sns/userinfo?access_token='.$access['access_token'].'&openid='.$access['openid'].'&lang=zh_CN'),true);
            if($userInfo['nickname']){
                $arr['nickname'] = $userInfo['nickname'];
                $arr['openid'] = $userInfo['openid'];
                $arr['head_img'] = $userInfo['headimgurl'];
                $arr['sex'] = $userInfo['sex'];
                $arr['token'] = md5($userInfo['openid'] . time() . $userInfo['nickname']);
                //$data['last_login_ip'] = getIP();
                $memberModel = new \app\api\model\Member;
                $id = $memberModel->allowField(true)->insertGetId($arr);
                if($id){
                    session('user_id',$id);
                    header('location:'.url('index/User/reg'));    //first用于判断是否完善信息,token用于vue端的信息验证
                }else{
                    echo '111';die;
                    print_r($data);
                }
            }else{
                echo '222';die;
            }
        }else{
            echo 333;die;
        }

    }

}